package au.org.htd.youslide.servlets;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import au.org.htd.youslide.Background;
import au.org.htd.youslide.PMF;
import au.org.htd.youslide.SongProject;
import au.org.htd.youslide.ppt.XMLSong;

@SuppressWarnings("serial")
public class DownloadServlet extends HttpServlet {
   private static final Logger log = Logger.getLogger(DownloadServlet.class
         .getName());

   public void doGet(HttpServletRequest req, HttpServletResponse resp)
         throws IOException {
      String requestType = req.getParameter("requestType");

      try {
         if ("slides".equals(requestType)) {
            doGetSlides(req, resp);
         } else if ("englishSlides".equals(requestType)) {
            doGetEnglishSlides(req, resp);
         } else if ("chineseSlides".equals(requestType)) {
            doGetChineseSlides(req, resp);
         } else if ("thumbnail".equals(requestType)) {
            doGetThumb(req, resp);
         } else if ("lyricsPdf".equals(requestType)) {
            doGetLyricsPdf(req, resp);
         } else {
            log.info("NO REQUEST TYPE!!!");
         }
      } catch (Exception e) {
         e.printStackTrace();
         throw new IOException(e);
      }
   }

   private void doGetThumb(HttpServletRequest req, HttpServletResponse resp)
         throws IOException {
      try {
         Long id = Long.parseLong(req.getParameter("id"));
         Background bg = new Background(PMF.getPM()).get(id);
         resp.setContentType(getServletContext().getMimeType(bg.getName()));
         resp.setContentLength(bg.getThumbnail().length);
         resp.getOutputStream().write(bg.getThumbnail());
         resp.getOutputStream().close();
         bg.closeConnection();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   private void doGetSlides(HttpServletRequest req, HttpServletResponse resp)
         throws Exception {
      Long id = Long.parseLong(req.getParameter("id"));
      SongProject project = new SongProject(PMF.getPM()).get(id);
      XMLSong song = new XMLSong(project);

      resp.setContentType(song.getContentType());
      resp.setCharacterEncoding("UTF-8");
      resp.setHeader("Content-Disposition", "attachment; filename=\""
            + project.getFileName() + song.getExtension() + "\"");
      song.writeSlides(System.out);
      resp.getWriter().write(song.getContent());
      project.closeConnection();
   }

   private void doGetEnglishSlides(HttpServletRequest req,
         HttpServletResponse resp) throws Exception {
      Long id = Long.parseLong(req.getParameter("id"));
      SongProject project = new SongProject(PMF.getPM()).get(id);
      XMLSong song = new XMLSong(project);

      resp.setContentType(song.getContentType());
      resp.setCharacterEncoding("UTF-8");
      resp.setHeader("Content-Disposition", "attachment; filename=\""
            + project.getEnglishFileName() + song.getExtension() + "\"");
      song.writeEnglishOnly(System.out);
      resp.getWriter().write(song.getContent());
      project.closeConnection();
   }

   private void doGetChineseSlides(HttpServletRequest req,
         HttpServletResponse resp) throws Exception {
      Long id = Long.parseLong(req.getParameter("id"));
      SongProject project = new SongProject(PMF.getPM()).get(id);
      XMLSong song = new XMLSong(project);

      resp.setContentType(song.getContentType());
      resp.setCharacterEncoding("UTF-8");
      resp.setHeader("Content-Disposition", "attachment; filename=\""
            + project.getChineseFileName() + song.getExtension() + "\"");
      song.writeChineseOnly(System.out);
      resp.getWriter().write(song.getContent());
      project.closeConnection();
   }

   private void doGetLyricsPdf(HttpServletRequest req, HttpServletResponse resp)
         throws Exception {
      Long id = Long.parseLong(req.getParameter("id"));
      SongProject project = new SongProject(PMF.getPM()).get(id);
      XMLSong song = new XMLSong(project);

      resp.setCharacterEncoding("UTF-8");
      resp.setContentType("application/pdf");
      resp.setHeader("Content-Disposition", "attachment; filename=\""
            + project.getFileName() + ".pdf" + "\"");
      song.writeLyricsPdf(resp.getOutputStream());
      project.closeConnection();
   }
}
